<?php
define("CCTRL", "category");
define("CCTRLNAME", "category");
define("TOKEN", "bb6222905ef8419183b5437779497596");
class category extends BACKEND_Controller {
    /*
      Project     : 48c6c450f1a4a0cc53d9585dc0fee742
      Created on  : Mar 16, 2013, 11:29:15 PM
      Author      : Truong Khuong - khuongxuantruong@gmail.com
      Description :
      Purpose of the stylesheet follows.
     */
    private $assigns=null;
    function __construct() {
        parent::__construct();
        
        $this->load->model('backend/category_model');
        $type=isset($_GET['type'])?$_GET['type']:null;
        //$this->category_model = new category_basemodel($type);
        $this->types = $this->category_model->get_by_type('system');
        $this->smarty = new CI_Smarty3();
        $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
        if (!isset($_SESSION["ADP"][CCTRL]["display"]))
            $_SESSION["ADP"][CCTRL]["display"] = 0;
        $this->assigns['CategoryType']=array('news'=>'News','project'=>'Project','system'=>'System','about'=>'About','product'=>'Product');
    }
    function viewport(){
        //print_r($_SESSION);die;
        echo $c=$this->load->view('views_backend/tree_maps/01_treemap',$this->assigns,true);
    }
    function index($type=null) {
        if(isset($_GET['view'])){$this->tree();return;}
        $assigns['categories'] = $this->category_model->catetory_list;
        $assigns['types']=$this->types;
        $assigns['menuactive']='menu-category';
        $colModel = $this->category_model->colmod();
        foreach ($colModel as $col) {
            $datafields[] = array(
                'name' => $col['datafield'],
                'type' => isset($col['type']) ? $col['type'] : 'string'
            );
            $column = $col;
            $column['editable'] = isset($col['editable']) ? $col['editable'] : false;
            $column['filterable'] = isset($col['filterable']) ? $col['filterable'] : true;
            $column['sortable'] = isset($col['sortable']) ? $col['sortable'] : true;
            $column['minwidth'] = isset($col['minwidth']) ? $col['minwidth'] : (isset($col['width']) ? $col['width'] : 100);
            $columns[$col['datafield']] = $column;
            $list[] = array(
                'label' => $col['text'],
                'value' => $col['datafield'],
                'checked' => isset($col["hidden"]) ? !$col["hidden"] : true
            );
        }
        $assigns['datafields']= $datafields;
        $assigns['columns']= $columns;
        $assigns['list']= $list;
        $assigns['type']=$type;
        $type=isset($_GET['type'])?$_GET['type']:$type;
        $assigns['bindingsource']="/admincp/backend/category/binding/$type";
        $assigns['treebindingsource']='/admincp/backend/category/treebinding/'.$type;
        $this->smarty
                ->assign($assigns)
                ->display("backend/03_category/03_grid");
    }
    function treebinding($type=null) {
        $result = $this->category_model->treebinding($type);
        $data=$result;
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($data));
    }
    function binding($type=null) {
        $result = $this->category_model->binding();
        $data=$result['rows'];//$this->category_model->get_by_type($type);
        $result['rows']=$this->category_model->buildTreeArray($data);
        $result['totalrecords']=  count($result['rows']);
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($result));
    }
    function editor($id = "") {
        $assigns = null;
        if ($id !== "")
            $assigns['item'] = $this->category_model->_get($id);
        $assigns['categories'] = $this->category_model->catetory_list;
        $assigns['types'] = $this->category_model->get_by_type('system');
        //$assigns['types']=$this->types;
        $this->smarty
                ->assign($assigns)
                ->display("backend/03_category/02_editor");
    }
    function getDataRow($Id=0){
        if($Id==0)$Id=(int)$this->input->post('Id');
        $R["result"] = -1;
        $R["message"] = "Bad request !.";
        if($Id>0){
            $R["result"] = 0;
            $R["message"] = "Get data Success !";
            $R["item"] = $this->category_model->_get($Id);
        }
        echo json_encode($R);
    }
    function loadEditPanel($type='',$Id=0){
        if($Id==0)$Id=(int)$this->input->post('Id');
        if($type=='')$type=$this->input->post('Type');
        if($Id>0)
            $this->assigns['item']=$this->category_model->_get($Id);
        
        $data=$this->category_model->get_by_type($type);
        $this->assigns['category_list']=$this->category_model->buildTreeArray($data);
        
        echo $c=$this->load->view('views_backend/10_categories/01_category_edit_panel',$this->assigns,true);
    }
    function save() {
        
        $R["result"] = -1;
        $R["message"] = ("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        if($this->privilege->aupr_permission!=777)goto result;
        $params=$this->input->post();
        $params["cate_parent"] = (int) $params["cate_parent"];
        $params["cate_position"] = (int) $params["cate_position"];
        $cate_id = (int)$params["cate_id"];unset($params["cate_id"]);
        if ($params["cate_parent"] != 0 && $cate_id !=0) {
            if (in_array($cate_id, $this->category_model->catetory_list[$params["cate_parent"]]->parents)){
                $R["message"] = 'Lỗi cấu trúc.';
                goto result;
            }
        }
        if ($cate_id != 0) {
            $rs = $this->category_model->_update($cate_id, $params);
        } else {
            $rs = $this->category_model->_insert($params);
        }
        if ($rs === true) {
            $R["result"] = 1;
            $R["message"] = ascii_to_entities("Dữ liệu đã được hệ thống ghi nhận.");
        } else {
            $R["result"] = -1;
            $R["error_number"] = $this->db->_error_number();
            $R["error_message"] = $this->db->_error_message();
            $R["message"] = ascii_to_entities("
                    Dữ liệu chưa được hệ thống ghi nhận.<br/>
                    Vui lòng kiểm tra dữ liệu và thử lại.<br/>
                    ");
            if ($R["error_number"] != 0)
                $R["message"].=ascii_to_entities("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
        }
        result:
        echo json_encode($R);
    }
    function store(){
		$dt=$this->category_model->binding();
		$result=$dt;
		$result['success']= true;
		header('Content-type: application/json');
		echo json_encode($result);
		return;
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($result));
    }
    function demo(){
        $data=$this->category_model->get_by_type('News');
        $arr=$this->buildTreeArray($data);
    }
    
}

?>